
local RoleDefine = require("role.RoleDefine")
local ObjMonster = require("core.ObjMonster")
local Buffer = require("buffer.Buffer")
local CombatCalc = require("skill.CombatCalc")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local Util = require("common.Util")

local HP_MAX = RoleDefine.HP_MAX
local ATK = RoleDefine.ATK
local DEF = RoleDefine.DEF
local HIT = RoleDefine.HIT
local SHANBI = RoleDefine.SHANBI
local BAOJI = RoleDefine.BAOJI
local RENXING = RoleDefine.RENXING
local LEVEL = RoleDefine.LEVEL
local SPEED = RoleDefine.SPEED
local HUIXIN_RATE = RoleDefine.HUIXIN_RATE
local FENG_ATK = RoleDefine.FENG_ATK
local FENG_KX = RoleDefine.FENG_KX
local FIRE_ATK = RoleDefine.FIRE_ATK
local FIRE_KX = RoleDefine.FIRE_KX
local WATER_ATK = RoleDefine.WATER_ATK
local WATER_KX = RoleDefine.WATER_KX
local LEI_ATK = RoleDefine.LEI_ATK
local LEI_KX = RoleDefine.LEI_KX
local HP_RATE = RoleDefine.HP_RATE
local SPEED_RATE = RoleDefine.SPEED_RATE
local ATK_RATE = RoleDefine.ATK_RATE
local DEF_RATE = RoleDefine.DEF_RATE
local HP_MAX_RATE = RoleDefine.HP_MAX_RATE
local ATTR_DEF_REN_XING_RATE = RoleDefine.ATTR_DEF_REN_XING_RATE

MonsterDyAttr = MonsterDyAttr or {}

function doCalc(monster, dyAttr)
    Buffer.calcsInit(monster)
    local attr = monster.attr
    for k in pairs(attr) do
        attr[k] = 0
    end
    
    if monster.pkTarget then
        CombatCalc.setAttrMount(monster,monster.pkTarget)   --保存主人属性
        monster.attr[SPEED] = monster.pkTarget.attr[SPEED]
    else    
        local conf = ObjMonster.get_conf(monster)
        -- from conf
        if dyAttr then
            calcDyAttr(monster, conf, dyAttr)
        end

        attr[HP_MAX]                        = conf.hp_max + (monster.init_hp_max or 0)
        attr[ATK]                           = conf.atk + (monster.init_atk or 0)
        attr[DEF]                           = conf.def + (monster.init_def or 0)
        attr[HIT]                           = conf.hit
        attr[SHANBI]                        = conf.shanbi
        attr[BAOJI]                         = conf.baoji
        attr[RENXING]                       = conf.renxing
        attr[LEVEL]                         = conf.lv
        attr[SPEED]                         = monster.base_speed
        attr[HUIXIN_RATE]                   = conf.huixin_rate          --会心一击
        attr[FENG_ATK]                      = conf.feng_atk             --风属性伤害
        attr[FENG_KX]                       = conf.feng_kx              --风属性抗性
        attr[FIRE_ATK]                      = conf.fire_atk             --火属性伤害
        attr[FIRE_KX]                       = conf.fire_kx              --火属性抗性
        attr[WATER_ATK]                     = conf.water_atk            --水属性伤害
        attr[WATER_KX ]                     = conf.water_kx             --水属性抗性
        attr[LEI_ATK]                       = conf.lei_atk              --雷属性伤害
        attr[LEI_KX]                        = conf.lei_kx               --雷属性抗性
        attr[ATTR_DEF_REN_XING_RATE]        = conf.kangbao_rate         --抗暴几率
        attr[HP_RATE]       = 1
     end

    Buffer.refreshRate(monster)

    -- rate attr
    attr[SPEED] = attr[SPEED] * (1 + (attr[SPEED_RATE] or 0))
    attr[ATK] = attr[ATK] * (1 + (attr[ATK_RATE] or 0))
    attr[DEF] = attr[DEF] * (1 + (attr[DEF_RATE] or 0))

    attr[HP_MAX] = math.floor(attr[HP_MAX] * (1 + (attr[HP_MAX_RATE] or 0)))

    if attr[HP_MAX] <1 then
        assert()
    end

    Buffer.refresh(monster)

    --tmp, add speed
    attr[SPEED] = attr[SPEED] + 60
    CombatCalc.calcAttr(monster)
end

function calcDyAttr(monster, conf, dyAttr)
    for k, v in pairs(dyAttr) do
        if k == HP_MAX  then
            if v[1] == 1 then
                monster.init_hp_max = conf.hp_max * v[2] 
            else
                monster.init_hp_max = conf.hp_max + v[2] 
            end
        elseif k == DEF then
            if v[1] == 1 then
                monster.init_def = conf.def * v[2] 
            else
                monster.init_def = conf.def  + v[2] 
            end
        elseif k == ATK then
            if v[1] == 1 then
                monster.init_atk = conf.atk * v[2] 
            else
                monster.init_atk = conf.atk + v[2] 
            end

        end
    end
end

function initDyAttr(monsterDyData)
    for k, v in pairs(monsterDyData) do
        MonsterDyAttr[k] = v 
    end
end

function updateFromW(monsterDyData)
    MonsterDyAttr[monsterDyData.monsterID] = monsterDyData 
end

function update(monsterDyData)
    MonsterDyAttr[monsterDyData.monsterID] = monsterDyData 
    local nSend2Wdata = {
        type = 1,
        data = MonsterDyAttr[monsterDyData.monsterID]
    }

    InnerDataManagerFL.Send2WInnerData(nil, InnerDataDefine.INNERDATA_TYPE_MONSTER, nSend2Wdata)
    --Util.printTable(MonsterDyAttr)
end

function getDyMonsterData(monsterID)
    return MonsterDyAttr[monsterID] or nil
end

function W2LProcess(human, type, oData)
    if oData.type == 1 then
        initDyAttr(oData.data)
    elseif oData.type == 2 then
        updateFromW(oData.data)
    end
end

InnerDataManagerFL.Register(InnerDataDefine.INNERDATA_TYPE_MONSTER, W2LProcess)
